/* mfcqa.p   - Session startup create QADDB, QAD aliases                      */
/* Copyright 1986 QAD Inc. All rights reserved.                               */
/* $Id:: mfcqa.p 15200 2012-02-23 20:06:30Z myb                            $: */
/* $Revision: 1.13 $                                                          */
/*                                                                            */
/* Description:                                                               */
/*  This program ensure that the QADDB and QAD aliases  are  set.             */
/*  It  returns  a  failure indication if the number of databases             */
/*  connected is zero.  If a failure indication is returned,  the             */
/*  RETURN ERROR condition is also raised which can be allowed to             */
/*  propagate  upward   or   trapped   by   the   caller.    This             */
/*  functionality was originally in mf.p but was separated into a             */
/*  separate program so  that  the  NetCommerce  session  startup             */
/*  could use it.                                                             */
/*                                                                            */
/* Revision: 1.7    BY: Bill Gates        DATE: 02/18/00  ECO: *N06R*         */
/* Revision: 1.8    BY: Mark Brown        DATE: 08/23/00  ECO: *N0ND*         */
/* Revision: 1.9    BY: Jean Miller       DATE: 10/23/00  ECO: *N0T3*         */
/* Revision: 1.12   BY: Jean Miller       DATE: 08/19/04  ECO: *Q0CG*         */
/* $Revision: 1.13 $     BY: Justin Zhang     DATE: 07/08/10 ECO: *R21R*  */
/******************************************************************************/
/* All patch markers and commented out code have been removed from the source */
/* code below. For all future modifications to this file, any code which is   */
/* no longer required should be deleted and no in-line patch markers should   */
/* be added.  The ECO marker should only be included in the Revision History. */
/******************************************************************************/

/*V8:ConvertMode=NoConvert                                                   */

{us/px/pxsevcon.i}

/* This parameter is needed when this program is used as the startup
   procedure for the appserver.  This parameter is not used in this program*/
define input parameter startupData as character no-undo.

define variable sdb as integer no-undo.

do on error undo, return {&GENERAL-APP-EXCEPT}:

   /*MUST HAVE DATABASE(S) CONNECTED*/
   if num-dbs = 0 then do:
      /* If databases are not connected, we cannot retrieve a message */
      /* Therefore, message must be hard-coded and not translated     */
      message "ERROR: No connected databases".
      undo, return error {&APP-ERROR-RESULT}.
   end.

   /* ASSIGN ALIASES QADDB AND  QAD.   ALIAS  QADDB  POINTS  TO  THE
      MFG/PRO  DATABASE  THUS  IF WE ARE RUNNING WITH A NON-PROGRESS DB
      THE ALIAS QADDB MUST POINT TO THE FOREIGN DATABASE NOT THE  FIRST
      CONNECTED DATABASE.  */
   if pdbname("qaddb") = ? then do:

      if not can-do(gateways,"oracle") then
         create alias qaddb for database value(ldbname(1)).

      else do:

         do sdb = 1 to num-dbs:
            if dbtype(sdb) <> "PROGRESS"
               and sdbname(sdb) = sdbname(ldbname(1))
            then do:
               create alias qaddb for database value(ldbname(sdb)).
               create alias qad for database value(ldbname(1)).
               leave.
            end.
         end.

         if ldbname(1) <> sdbname("qaddb") then
            create alias qaddb for database value(ldbname(1)).

      end.

   end.

   /* IF LDBNAME("QADDB") <> ? THEN MIGHT NEED TO RESET ALIASES */
   else if sdbname("qaddb") <> ldbname(1) then do:
      do sdb = 1 to num-dbs:

         if dbtype(sdb) <> "PROGRESS" and
            sdbname(sdb) = sdbname(ldbname(1))
         then do:

            if pdbname("qaddb") <> pdbname(ldbname(sdb)) then
               create alias qaddb for database value(ldbname(sdb)).

            if pdbname("qad") <> pdbname(ldbname(1)) then
               create alias qad for database value(ldbname(1)).

            leave.
         end.
      end.

      if ldbname(1) <> sdbname("qaddb") then
         create alias qaddb for database value(ldbname(1)).

   end.

   /* IF ALIAS QADDB FOR FOREIGN DB IS SET BUT ALIAS QAD IS NOT */
   if sdbname("qaddb") = ldbname(1) and
      dbtype("qaddb") <> "PROGRESS" and
      pdbname("qad") <> pdbname(ldbname(1))
   then
      create alias qad for database value(ldbname(1)).

   return {&SUCCESS-RESULT}.
end.
